﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsBusinessSubCategoryTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{

    /// <summary>
    /// Business Sub Category Business Object
    /// </summary>
   public class BusinessSubCategory:BOBase
    {

       #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
       protected BusinessSubCategory() { }

        #endregion

       #region Public Properties

       #region Instance Properties

       /// <summary>
       /// PKID of the BusinessSubCategory.
       /// </summary>
       public int BusinessSubCatgryID
       {
           get { return _businesssubcatgryId; }
           set
           {
               if (_businesssubcatgryId != value)
               {
                   if (OnPropertyChanging("BusinessSubCatgryID", _businesssubcatgryId, value))
                   {
                       int oldValue = _businesssubcatgryId;
                       _businesssubcatgryId = value;

                       OnPropertyChanged("BusinessSubCatgryID", oldValue, value);
                   }
               }
           }
       }  private int _businesssubcatgryId = -1;


       /// <summary>
       /// PKID of the Selected BusinessCategory.
       /// </summary>
       public int BusinessCatgryID
       {
           get { return _businesscatgryId; }
           set
           {
               if (_businesscatgryId != value)
               {
                   if (OnPropertyChanging("BusinessCatgryID", _businesscatgryId, value))
                   {
                       int oldValue = _businesscatgryId;
                       _businesscatgryId = value;

                       OnPropertyChanged("BusinessCatgryID", oldValue, value);
                   }
               }
           }
       }  private int _businesscatgryId = -1;



       /// <summary>
       /// Business Sub Category Value.
       /// </summary>
       public string BusinessSubCatgryName
       {
           get { return _businesssubcatgryname; }
           set
           {
               if (_businesssubcatgryname != value)
               {
                   if (OnPropertyChanging("BusinessSubCatgryName", _businesssubcatgryname, value))
                   {
                       string oldValue = _businesssubcatgryname;
                       _businesssubcatgryname = value;
                       OnPropertyChanged("BusinessSubCatgryName", oldValue, value);
                   }
               }
           }
       }  private string _businesssubcatgryname = null;


       /// <summary>
       /// Business Sub Category Description.
       /// </summary>
       public string BusinessSubCatgryDescription
       {
           get { return _businesssubcatgrydescription; }
           set
           {
               if (_businesssubcatgrydescription != value)
               {
                   if (OnPropertyChanging("BusinessSubCatgryDescription", _businesssubcatgrydescription, value))
                   {
                       string oldValue = _businesssubcatgrydescription;
                       _businesssubcatgrydescription = value;
                       OnPropertyChanged("BusinessSubCatgryDescription", oldValue, value);
                   }
               }
           }
       }  private string _businesssubcatgrydescription = null;


       /// <summary>
       /// Business Sub Category AddBy.
       /// </summary>
       public string BusinessSubCatgryAddBy
       {
           get { return _businesssubcatgryaddby; }
           set
           {
               if (_businesssubcatgryaddby != value)
               {
                   if (OnPropertyChanging("BusinessSubCatgryAddBy", _businesssubcatgryaddby, value))
                   {
                       string oldValue = _businesssubcatgryaddby;
                       _businesssubcatgryaddby = value;
                       OnPropertyChanged("BusinessSubCatgryAddBy", oldValue, value);
                   }
               }
           }
       }  private string _businesssubcatgryaddby = null;

       /// <summary>
       /// Date/time at which the BusinessSubCategory was created.
       /// </summary>
       public DateTime? BusinessSubCatgryDateAdded
       {
           get { return _businesssubcatgrydateadded; }
           set
           {
               if (_businesssubcatgrydateadded != value)
               {
                   if (OnPropertyChanging("BusinessSubCatgryDateAdded", _businesssubcatgrydateadded, value))
                   {
                       DateTime? oldValue = _businesssubcatgrydateadded;
                       _businesssubcatgrydateadded = value;
                       OnPropertyChanged("BusinessSubCatgryDateAdded", oldValue, value);
                   }
               }
           }
       }  private DateTime? _businesssubcatgrydateadded = null;

       /// <summary>
       /// Date/time at which the BusinessSubCategory was updated.
       /// </summary>
       public DateTime? BusinessSubCatgryDateModified
       {
           get { return _businesssubcatgrydatemodified; }
           set
           {
               if (_businesssubcatgrydatemodified != value)
               {
                   if (OnPropertyChanging("BusinessSubCatgryDateModified", _businesssubcatgrydatemodified, value))
                   {
                       DateTime? oldValue = _businesssubcatgrydatemodified;
                       _businesssubcatgrydatemodified = value;
                       OnPropertyChanged("BusinessSubCatgryDateModified", oldValue, value);
                   }
               }
           }
       }  private DateTime? _businesssubcatgrydatemodified = null;


       /// <summary>
       /// True iff the Admin has made this BusinessSubCategory Active (i.e. to be used in the application).
       /// </summary>
       public bool BusinessSubCatgryIsActive
       {
           get { return _businesssubcatgryIsactive; }
           set
           {
               if (_businesssubcatgryIsactive != value)
               {
                   if (OnPropertyChanging("BusinessSubCatgryIsActive", _businesssubcatgryIsactive, value))
                   {
                       bool oldValue = _businesssubcatgryIsactive;
                       _businesssubcatgryIsactive = value;
                       OnPropertyChanged("BusinessSubCatgryIsActive", oldValue, value);
                   }
               }
           }
       }  private bool _businesssubcatgryIsactive = false;

       #endregion

       #endregion

       #region Public Methods

       #region CreateNew

       /// <summary>
       /// Creates a new, empty BusinessSubCategory object.
       /// </summary>
       public static BusinessSubCategory CreateNew()
       {
           BusinessSubCategory obj = new BusinessSubCategory();

           // Switch state to Added prior to initializing property values.
           obj.ObjState = ObjectState.Added;

           obj.BusinessSubCatgryDateAdded = DateTime.Now;
           obj.BusinessSubCatgryDateModified = DateTime.Now;
           obj.BusinessSubCatgryIsActive = true;
           return (obj);
       }


       [MethodImpl(MethodImplOptions.Synchronized)]
       public static BusinessSubCategory CreateNew(BusinessSubCategoryCollection collection)
       {
           BusinessSubCategory obj = CreateNew();

           // Get next/unique ID for the new object.
           int minVal = 0;
           foreach (BusinessSubCategory businesssubcategory in collection)
           {
               if (businesssubcategory.BusinessSubCatgryID < minVal)
                   minVal = businesssubcategory.BusinessSubCatgryID;
           }
           obj.BusinessSubCatgryID = --minVal;
           collection.Add(obj);

           return (obj);
       }

       #endregion

       #region GetAllBusinessSubCategories

       /// <summary>
       /// Returns a collection of all BusinessSubCategory.
       /// </summary>
       public static BusinessSubCategoryCollection GetAllBusinessSubCategories()
       {
         BusinessSubCatagoryTableAdapter adapter = new BusinessSubCatagoryTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();

           dsBusinessSubCategory.BusinessSubCatagoryDataTable tbl = adapter.GetData(null);

           BusinessSubCategoryCollection list = new BusinessSubCategoryCollection();

           if (tbl.Rows.Count > 0)
           {
               foreach (dsBusinessSubCategory.BusinessSubCatagoryRow row in tbl.Rows)
               {
                   BusinessSubCategory businessSubcategory = new BusinessSubCategory();
                   RelationalToObject(businessSubcategory, row);
                   list.Add(businessSubcategory);
               }
           }

           return (list);
       }

       #endregion

       #region GetBusinessSubCategoryByID

       /// <summary>
       /// Returns an BusinessSubCategory object for the specified 'BusinessSubCategoryID' or null if no BusinessSubCategory with the specified parameter(s) exists.
       /// </summary>
       public static BusinessSubCategory GetBusinessSubCategoryByID(int businessSubcategoryid)
       {
          BusinessSubCatagoryTableAdapter adapter = new BusinessSubCatagoryTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();
           dsBusinessSubCategory.BusinessSubCatagoryDataTable tbl = adapter.GetData(businessSubcategoryid);

           if (tbl.Rows.Count == 1)
           {
               BusinessSubCategory bsc = new BusinessSubCategory();
               RelationalToObject(bsc, tbl.Rows[0]);
               return (bsc);
           }
           else if (tbl.Rows.Count > 1)
               throw new ApplicationException("Database integrity error: Specified 'SubCategory' returned more than one record.");

           return (null);
       }

       #endregion

       #region GetAllBusinessSubCategoriesByBusinessCategoryID

       /// <summary>
       /// Returns a collection of all BusinessSubCategory by BusinessCategoryID.
       /// </summary>
       public static BusinessSubCategoryCollection GetAllBusinessSubCategoriesByBusinessCategoryID(int businesscategoryid)
       {
          BusinessSubCatagoryTableAdapter adapter = new BusinessSubCatagoryTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();

           dsBusinessSubCategory.BusinessSubCatagoryDataTable tbl = adapter.GetDataByBusinessCatgryID(businesscategoryid);

           BusinessSubCategoryCollection list = new BusinessSubCategoryCollection();

           if (tbl.Rows.Count > 0)
           {
               foreach (dsBusinessSubCategory.BusinessSubCatagoryRow row in tbl.Rows)
               {
                   BusinessSubCategory businessSubcategory = new BusinessSubCategory();
                   RelationalToObject(businessSubcategory, row);
                   list.Add(businessSubcategory);
               }
           }

           return (list);
       }

       #endregion

       #region GetBusinessSubCategoryByBusinessSubCatgryName

       /// <summary>
       /// Returns an BusinessCategory object for the specified 'BusinessCatgryName' or null if no BusinessCategory with the specified parameter(s) exists.
       /// </summary>
       public static BusinessSubCategory GetBusinessSubCategoryByBusinessSubCatgryName(string businesssubcatgryname)
       {
           BusinessSubCatagoryTableAdapter adapter = new BusinessSubCatagoryTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();
           dsBusinessSubCategory.BusinessSubCatagoryDataTable tbl = adapter.GetDataByBusinessSubCatgryName(businesssubcatgryname);

           if (tbl.Rows.Count == 1)
           {
               BusinessSubCategory bsc = new BusinessSubCategory();
               RelationalToObject(bsc, tbl.Rows[0]);
               return (bsc);
           }
           else if (tbl.Rows.Count > 1)
               throw new ApplicationException("Database integrity error: Specified 'businesssubcategory' returned more than one businesssubcategory.");

           return (null);
       }

       #endregion

       #region Update

       /// <summary>
       /// Updates the specified BusinessSubCategory.
       /// </summary>
       public static void Update(BusinessSubCategory obj)
       {
           bool updateBusinessSubCategory = false;

           // Do not update invalid objects.
           if (!obj.Validate())
           {
               StringBuilder sb = new StringBuilder(2048);
               sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
               foreach (ValidationError err in obj.Errors)
                   sb.Append("\n* " + err.Element + ": " + err.Message);

               throw new InvalidObjectException(sb.ToString());
           }
           else
           {
               updateBusinessSubCategory = (obj.ObjState != ObjectState.Unchanged);

               if (!updateBusinessSubCategory)
                   return;
           }

           ObjectState originalObjectState = obj.ObjState;

           dsBusinessSubCategory.BusinessSubCatagoryDataTable tbl = null;
           if (updateBusinessSubCategory)
           {

              BusinessSubCatagoryTableAdapter adapter = new BusinessSubCatagoryTableAdapter();
               adapter.Connection = AppSettings.GetDbConn();


               tbl = new dsBusinessSubCategory.BusinessSubCatagoryDataTable();
               dsBusinessSubCategory.BusinessSubCatagoryRow row = tbl.NewBusinessSubCatagoryRow();
               ObjectToRelational(obj, tbl, row);

               adapter.Update(tbl);

               if (originalObjectState == ObjectState.Deleted)
                   obj = null;
               else RelationalToObject(obj, tbl.Rows[0]);
           }
       }


       #endregion

       #region Validate

       /// <summary>
       /// Validates the object based on its properties at the time of the call; updates the Errors property.
       /// </summary>
       public override bool Validate()
       {
           Errors.Clear();

           if (ObjState == ObjectState.Unchanged)
           {
               // By definition, unchanged objects are valid.
               return (true);
           }
           else if (ObjState == ObjectState.Uninitialized)
           {
               Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

               // No need to proceed past this error.
               return (false);
           }
           else if ((ObjState == ObjectState.Deleted) && (BusinessCatgryID <= 0))
           {
               // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
               Errors.Add(new ValidationError("BusinessSubCatgryID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

               // No need to proceed past this error.
               return (false);
           }
           else  // ObjState == Added | Modified
           {

               // SubCategoryName.
               if ((BusinessSubCatgryName == null) || (BusinessSubCatgryName.Trim().Length == 0))
               {
                   Errors.Add(new ValidationError("BusinessSubCatgryName", "BusinessSubCatgryName is required.", ValidationError.Severity.Error));
               }
               else if (ObjState == ObjectState.Added)
               {

               }

           }

           return (Errors.Count == 0);
       }

       #endregion

       #region ToString

       /// <summary>
       /// User-friendly string represetation of this object.
       /// </summary>
       public override string ToString()
       {
           return (String.Format("BusinessSubCategory: BSCID={0}, BusinessSubCatgryName={1}, {2}", BusinessSubCatgryID, BusinessSubCatgryName, BusinessSubCatgryDescription));
       }

       #endregion

       #endregion

    }

   /// <summary>
   /// Collection of 0 or more BusinessSubCategory objects.
   /// </summary>

   public class BusinessSubCategoryCollection : List<BusinessSubCategory>
   {
       #region Types

       public class UserComparer : IComparer<BusinessSubCategory>
       {
           public int Compare(BusinessSubCategory jsc1, BusinessSubCategory jsc2)
           {
               return (String.Compare(jsc1.BusinessSubCatgryName, jsc2.BusinessSubCatgryName));
           }
       }

       #endregion

       #region Public Methods

       #region FindByBusinessSubCategoryID

       public BusinessSubCategory FindByBusinessSubCategoryID(int businesssubcategoryID)
       {
           for (int ndx = 0; ndx < this.Count; ndx++)
               if (this[ndx].BusinessSubCatgryID == businesssubcategoryID)
                   return (this[ndx]);

           return (null);
       }

       #endregion

       #region FindUserByBusinessSubCategoryName

       /// <summary>
       /// Case insensitve compare.
       /// </summary>
       public BusinessSubCategory FindUserByBusinessSubCategory(string businesssubcategoryname)
       {
           for (int ndx = 0; ndx < this.Count; ndx++)
               if (this[ndx].BusinessSubCatgryName == businesssubcategoryname)
                   return (this[ndx]);

           return (null);
       }

       #endregion

       #region FindSubCategoryByCountBusinessSubCategoryID

       /// <summary>
       /// Case insensitve compare.
       /// </summary>
       public int FindSubCategoryByCountBusinessSubCategoryID(int subcategoryId)
       {
           int count = 0;
           for (int ndx = 0; ndx < this.Count; ndx++)
               if (this[ndx].BusinessSubCatgryID == subcategoryId)
                   count++;

           return (count);
       }

       #endregion

       #endregion

   }
}
